// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

#include "Import/BDFImport/BulkData/Element/CSHEARParser.h"

#include <algorithm>
#include <cstdint>
#include <iostream>

#include "Utility/Logging/SGLogger.h"

#include "DataStructure/Input/Element/CSHEARData.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Element/IElementService.h"

#include "Import/BDFImport/BDFParserBase.h"

BULKDATA_ENTRY_PARSER_REG (CSHEAR);

using namespace SG::DBManager;
using namespace SG::Import;
using namespace SG::DataStructure;
/*
// file://./../../Doc/MarkDown/BulkData/CSHEAR/CSHEAR.md>
*/
enum CSHEARIndex : std::uint8_t
{
    CSHEARIndex_EID = 1,
    CSHEARIndex_PID = 2,
    CSHEARIndex_G1  = 3,
    CSHEARIndex_G2  = 4,
    CSHEARIndex_G3  = 5,
    CSHEARIndex_G4  = 6,
};

SG::DataStructure::Common::Status BDF::BULKDATA::ParseCSHEAR (const std::shared_ptr<SG::DBManager::DBServiceFactory>& dbServiceFactory,
                                                              std::vector<std::string>&                               dividedEntryInfo)
{
    // 创建临时的CSHEAR数据对象
    SG::DataStructure::FEM::CSHEARData tempCSHEAR;

    // TODO(bug 001) m_dominId的正确填写
    tempCSHEAR.m_domainId = 1;  // 设置m_domainId为1，待修复

    // 解析并存储CSHEAR条目信息
    // 解析EID（元素识别号）
    tempCSHEAR.m_id = convertToI64 (dividedEntryInfo[CSHEARIndex_EID]);

    // 解析PID（属性识别号）
    // 如果PID为空，则默认PID等于EID
    if (std::all_of (
            dividedEntryInfo[CSHEARIndex_PID].begin (), dividedEntryInfo[CSHEARIndex_PID].end (), [] (char c) { return c == ' '; }))
    {
        tempCSHEAR.m_pId = tempCSHEAR.m_id;
    }
    else
    {
        tempCSHEAR.m_pId = convertToI64 (dividedEntryInfo[CSHEARIndex_PID]);
    }

    // 解析网格点G1到G4
    tempCSHEAR.m_g[0] = convertToI64 (dividedEntryInfo[CSHEARIndex_G1]);
    tempCSHEAR.m_g[1] = convertToI64 (dividedEntryInfo[CSHEARIndex_G2]);
    tempCSHEAR.m_g[2] = convertToI64 (dividedEntryInfo[CSHEARIndex_G3]);
    tempCSHEAR.m_g[3] = convertToI64 (dividedEntryInfo[CSHEARIndex_G4]);

    // 将解析后的CSHEAR数据添加到数据库中
    return dbServiceFactory->get<IElementService> ()->Add (tempCSHEAR);
}